#pragma once

#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;

namespace Joy
{
    class string
    {
    public:
        using iterator = char *;
        using const_iterator = const char *;
        const static size_t npos = -1;

        string(const char *str = "")
        {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_capacity + 1];

            strcpy(_str, str);
        }

        ~string()
        {
            delete[] _str;
            _size = _capacity = 0;
        }

        const char *c_str() const
        {
            return _str;
        }

        size_t size() const
        {
            return _size;
        }

        size_t capacity() const
        {
            return _capacity;
        }

        size_t reserve(size_t n)
        {
            if (n > _capacity)
            {
                char *tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;
                _capacity = n;
                _str = tmp;
            }
        }

        void resize(size_t n, char ch = '\0')
        {
            if (n > _size)
            {
                reserve(n);
                for (size_t i = _size; i < n; ++i)
                {
                    _str[i] = ch;
                }

                _size = n;
                _str[n] = '\0';
            }
            else
            {
                _str[n] = '\0';
                _size = n;
            }
        }

        char &operator[](size_t pos)
        {
            assert(pos < _size);
            return _str[pos];
        }

        const char &operator[](size_t pos) const
        {
            assert(pos < _size);
            return _str[pos];
        }

        iterator begin()
        {
            return _str;
        }

        const_iterator begin() const
        {
            return _str;
        }

        iterator end()
        {
            return _str + _size;
        }

        const_iterator end() const
        {
            return _str + _size;
        }

        string &push_back(char ch)
        {
            if (_size == _capacity)
            {
                int newCapacity = _capacity == 0 ? 4 : 2 * _capacity;
                reserve(newCapacity);
            }
            _str[_size] = ch;
            ++_size;
            _str[_size] = '\0';

            return *this;
        }

        string &append(const char *str)
        {
            size_t len = strlen(str);
            if (len + _size > _capacity)
            {
                reserve(len + _size);
            }

            strcpy(_str + _size, str);
            _size += len;

            return *this;
        }

        string &operator+=(char ch)
        {
            push_back(ch);
            return *this;
        }

        string &operator+=(const char *str)
        {
            append(str);
            return *this;
        }

        string &insert(size_t pos, char ch)
        {
            assert(pos <= _size);

            if (_size == _capacity)
            {
                int newCapacity = _capacity == 0 ? 4 : 2 * _capacity;
                reserve(newCapacity);
            }

            size_t end = _size + 1;
            while (end > pos)
            {
                _str[end] = _str[end - 1];
                --end;
            }

            _str[pos] = ch;
            ++_size;

            return *this;
        }

        string &insert(size_t pos, const char *str)
        {
            assert(pos <= _size);

            size_t len = strlen(str);
            if (len + _size > _capacity)
            {
                reserve(len + _size);
            }

            size_t end = _size + len;
            while (end > pos + len - 1)
            {
                _str[end] = _str[end - len];
                --end;
            }

            strncpy(_str + pos, str, len);
            _size += len;

            return *this;
        }

        bool empty() const
        {
            return _size == 0;
        }

        string &erase(size_t pos, size_t len = npos)
        {
            assert(!empty());
            assert(pos < _size);

            if (len == npos || pos + len >= _size)
            {
                _str[pos] = '\0';
                _size = pos;
            }
            else
            {
                strcpy(_str + pos, _str + pos + len);
                _size -= len;
            }

            return *this;
        }

        size_t find(char ch, size_t pos) const
        {
            assert(pos < _size);
            while (pos < _size)
            {
                if (_str[pos] == ch)
                    return pos;
                ++pos;
            }
            return npos;
        }

        size_t find(const char *str, size_t pos) const
        {
            assert(pos < _size);
            const char *ptr = strstr(_str + pos, str);
            if (ptr == nullptr)
                return npos;
            else
                return ptr - _str;
        }

        void clear()
        {
            _size = 0;
            _str[0] = '0';
        }

        // //传统拷贝构造写法 s2(s1)
        // string(const string& s)
        // {
        //     _str = new char[s._capacity + 1];
        //     _capacity = s._capacity;
        //     _size = s._size;

        //     strcpy(_str, s._str);
        // }

        // // 传统赋值运算符重载 s2 = s1
        // string& operator=(const string& s)
        // {
        //     if (this != &s)
        //     {
        //         char* tmp = new char[s._capacity + 1];
        //         strcpy(tmp, s._str);

        //         delete[] _str;
        //         _str = tmp;

        //         _size = s._size;
        //         _capacity = s._capacity;
        //     }
        //     return *this;
        // }

        void swap(string& s)
        {
            std::swap(_str, s._str);
            std::swap(_size, s._size);
            std::swap(_capacity, s._capacity);
        }

        // 现代拷贝构造写法 s2(s1)
        string(const string& s)
            : _str(nullptr)
            , _size(0)
            , _capacity(0)
        {
            string tmp(s._str); // 构造函数
            //this->swap(tmp);
            swap(tmp);
        }

        string& operator=(string s)
        {
            swap(s);
            return *this;
        }

    private:
        char *_str;
        size_t _size;
        size_t _capacity;
    };

    ostream &operator<<(ostream &out, const string &s)
    {
        for (size_t i = 0; i < s.size(); i++)
        {
            out << s[i];
        }
        return cout;
    }

    istream &operator>>(istream &in, string &s)
    {
        s.clear();

        /*char ch = in.get();
        while (ch != ' ' && ch != '\n')
        {
            s += ch;
            //in >> ch;
            ch = in.get();
        }
        return in;*/

        char buff[128] = {'\0'};
        size_t i = 0;
        char ch = in.get();
        while (ch != ' ' && ch != '\n')
        {
            if (i == 127)
            {
                s += buff;
                i = 0;
            }

            buff[i++] = ch;
            ch = in.get();
        }

        if (i > 0)
        {
            buff[i] = '\0';
            s += buff;
        }

        return in;
    }
}